/// 🎯 活动参与者实体模型
/// 
/// 核心业务对象，代表活动参与者数据
/// 对应接口: /publisher/activity/participants/
class ParticipantEntity {
  /// 用户ID
  final int userId;
  
  /// 用户昵称
  final String userName;
  
  /// 用户年龄
  final int? userAge;
  
  /// 用户性别
  final String? userGender;
  
  /// 用户头像URL
  final String? userAvatarUrl;
  
  /// 用户签名
  final String? userSignature;
  
  /// 活动ID
  final int activityId;
  
  /// 活动名称
  final String activityName;
  
  /// 申请时间
  final String applicationTime;
  
  /// 参与者状态：pending（待审核）、approved（已通过）、rejected（已拒绝）
  final String participantStatus;
  
  /// 用户等级（实际存储的是积分）
  final int? userLevel;
  
  /// 用户生日
  final String? userBirthday;
  
  /// 额外数据
  final Map<String, dynamic>? extraData;

  ParticipantEntity({
    required this.userId,
    required this.userName,
    this.userAge,
    this.userGender,
    this.userAvatarUrl,
    this.userSignature,
    required this.activityId,
    required this.activityName,
    required this.applicationTime,
    required this.participantStatus,
    this.userLevel,
    this.userBirthday,
    this.extraData,
  });

  /// 是否待审核
  bool get isPending => participantStatus == 'pending' || participantStatus == '待审核';
  
  /// 是否已通过
  bool get isApproved => participantStatus == 'approved' || participantStatus == '已通过';
  
  /// 是否已拒绝
  bool get isRejected => participantStatus == 'rejected' || participantStatus == '已拒绝';

  /// 从JSON创建实例
  factory ParticipantEntity.fromJson(Map<String, dynamic> json) {
    // 状态映射：根据reviewStatus和isApproved字段确定参与者状态
    String status = 'pending'; // 默认状态
    final reviewStatus = json['reviewStatus'] as String?;
    final isApproved = json['isApproved'] as bool?;
    
    if (reviewStatus == '审核中' || reviewStatus == 'pending') {
      status = 'pending';
    } else if (isApproved == true || reviewStatus == '已通过' || reviewStatus == 'approved' || reviewStatus == '通过') {
      status = 'approved';
    } else if (isApproved == false || reviewStatus == '已拒绝' || reviewStatus == 'rejected' || reviewStatus == '未通过' || reviewStatus == '拒绝') {
      status = 'rejected';
    }
    
    return ParticipantEntity(
      userId: json['userId'] is String ? int.parse(json['userId']) : (json['userId'] as int),
      userName: json['username'] ?? '未知用户',
      userAge: json['userAge'] as int?,
      userGender: json['userGender'] as String?,
      userAvatarUrl: json['userAvatar'] as String?, // 注意字段名是userAvatar而不是userAvatarUrl
      userSignature: json['userSignature'] as String?,
      activityId: json['activityId'] is String ? int.parse(json['activityId']) : (json['activityId'] as int),
      activityName: json['activityTitle'] ?? '未知活动',
      applicationTime: json['joinTime'] ?? '',
      participantStatus: status, // 使用计算出的状态
      userLevel: json['userPoints'] as int?, // 使用userPoints作为等级
      userBirthday: json['userBirthday'] as String?,
      extraData: json['userExtraData'] as Map<String, dynamic>?,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'user_id': userId,
      'user_name': userName,
      if (userAge != null) 'user_age': userAge,
      if (userGender != null) 'user_gender': userGender,
      if (userAvatarUrl != null) 'user_avatar_url': userAvatarUrl,
      if (userSignature != null) 'user_signature': userSignature,
      'activity_id': activityId,
      'activity_name': activityName,
      'application_time': applicationTime,
      'participant_status': participantStatus,
      if (userLevel != null) 'user_level': userLevel,
      if (userBirthday != null) 'user_birthday': userBirthday,
      if (extraData != null) 'extra_data': extraData,
    };
  }

  @override
  String toString() {
    return 'ParticipantEntity{userId: $userId, userName: $userName, activityName: $activityName, participantStatus: $participantStatus}';
  }
}

